package com.yuchen.ishop.web.admin;

import com.yuchen.core.MyPager;
import com.yuchen.ishop.entity.Buyer;
import com.yuchen.ishop.service.admin.buyer.BuyerService;
import com.yuchen.ishop.web.BaseAction;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 *
 */
@Controller
@RequestMapping(value = "/admin/buyer")
public class BuyerAction extends BaseAction {

    private final static Logger LOG = Logger.getLogger(BuyerAction.class);

    @Autowired
    private BuyerService buyerService;

    @RequestMapping(value = "/index")
    public String index(MyPager myPager,ModelMap modelMap,HttpServletRequest request)throws Exception {
        Map params =convertParameterMap(request.getParameterMap()) ;
        modelMap.addAttribute("pager",buyerService.find(params,myPager));
        return "admin/buyer/index";
    }

    @RequestMapping(value = {"/add", "/edit"})
    public String add_edit(Long id,ModelMap modelMap)throws Exception {
        if(null == id) {
            return "admin/buyer/add";
        }
        else{
            modelMap.addAttribute("buyer",buyerService.getById(id));
            return "admin/buyer/edit";
        }
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(Buyer buyer)throws Exception {
        buyerService.insert(buyer);
        return "redirect:/admin/buyer/index.do";
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Buyer buyer,Long pageNo)throws Exception {
        buyerService.update(buyer,true);
        if(null == pageNo || pageNo<1) pageNo = 1l;
        return "redirect:/admin/buyer/index.do?pageNo="+pageNo;
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(Long id,Long pageNo)throws Exception {
        buyerService.delete(id);
        if(null == pageNo || pageNo<1) pageNo = 1l;
        return "redirect:/admin/buyer/index.do?pageNo="+pageNo;
    }

  

}
